﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using ExitGames.Client.Photon;

namespace TestClient
{
    public class PhotonListener : IPhotonPeerListener
    {
        public MainWindow Main;
        public void DebugReturn(DebugLevel level, string message)
        {
            Main.Dispatcher.Invoke((Action) (() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - DebugReturn called with: " + level + " - " + message + "\n");
            }));

        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            Main.Dispatcher.Invoke((Action) (() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - OnOperationResponse called with: " + operationResponse.OperationCode +
                                              " - " +
                                              operationResponse.ReturnCode + " - " + operationResponse.DebugMessage +
                                              "\n");
            }));
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            Main.Dispatcher.Invoke((Action) (() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - OnStatusChanged called with: " + statusCode + "\n");
                if (statusCode == StatusCode.Connect)
                {
                    Main.Disconnect_Button.IsEnabled = true;
                    Main.Login_Button.IsEnabled = true;
                    Main.PeerConnected = true;
                }
                else
                {
                    Main.Disconnect_Button.IsEnabled = false;
                    Main.Login_Button.IsEnabled = false;
                    Main.Connect_Button.IsEnabled = true;
                    Main.StatusIndicator.Fill = new SolidColorBrush(Colors.Red);
                    Main.PeerConnecting = false;
                    Main.PeerConnected = false;
                    Main.DispatcherTimer.Stop();
                }
            }));
        }

        public void OnEvent(EventData eventData)
        {
            Main.Dispatcher.Invoke((Action) (() =>
            {
                Main.StatusTextBox.AppendText(DateTime.Now.ToString("yy-mm-dd hh.mm.ss.ffffff") + " - OnEvent called with: " + eventData.Code);
            }));
        }
    }
}
